package com.frogs.ucenter.component;

import java.util.Arrays;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.frogs.ucenter.entity.UserShippingAddress;
import com.frogs.ucenter.service.AreaService;
import com.frogs.ucenter.service.ShoppingCartService;
import com.frogs.ucenter.service.UserService;
import com.frogs.ucenter.service.UserShippingAddressService;

@Component("rsyncHelper")
public class RsyncHelper {
	
	private Logger log = LoggerFactory.getLogger(RsyncHelper.class);
	@Resource
	private UserService userService;
	
	@Resource
	private UserShippingAddressService userShippingAddressService;
	
	@Resource
	private AreaService areaService;
	
	@Resource
	private ShoppingCartService shoppingCartService;
	
	@Async
	public void rsyncPhoneAddress(final Integer userId, final String phone) throws Exception {
		try {
			JSONObject json = userShippingAddressService.getAddressByPhone(userId, phone);
			if(json.getJSONObject("response").containsKey("list")) {
				JSONArray jsa = json.getJSONObject("response").getJSONArray("list");
				for(int i = 0; i < jsa.size(); i++) {
					JSONObject jsonObj = jsa.getJSONObject(i);
					UserShippingAddress userShippingAddress = new UserShippingAddress();
					String city = jsonObj.getString("cityName");
					String province = jsonObj.getString("provinceName");
					String district = jsonObj.getString("districtName");
					String detailAddr = jsonObj.getString("detailAddr");
					
					userShippingAddress.setCity(city);
					userShippingAddress.setProvince(province);
					userShippingAddress.setDistrict(district);
					userShippingAddress.setStreet(detailAddr.replace(province, "").replace(city, "").replace(district, "").replace(province + "省", "").replace(city + "市", "").replace(district + "区", ""));
					userShippingAddress.setContacts(jsonObj.getString("receiveName"));
					userShippingAddress.setContactNumber(jsonObj.getString("mobile"));
					userShippingAddress.setSource(jsonObj.getString("produceSource"));
					userShippingAddress.setTelNumber(jsonObj.getString("telphone"));
					
					JSONObject area = areaService.getAreaInfo(province, city, district);
					if(null != area) {
						userShippingAddress.setAreaId(area.getString("areaId"));
					}
					//userShippingAddress.setName(OrderProduceSourceEnum.getMap().get(jsonObj.getString("produceSource")));
					
					userShippingAddressService.addUserAddress(userId, userShippingAddress);
				}
			}
		} catch (Exception e) {
			log.error("func[rsyncPhoneAddress] err[" + e.getMessage() + "]",e);
		}
	}
	
	@Async
	public void mergeShoppingCart(Integer userId, String userTraceId) {
		try {
			shoppingCartService.mergeShoppingCart(userId, userTraceId);
		} catch (Exception e) {
			log.error("func[mergeShoppingCart] userId[{}] userTraceId[{}] exception[{} - {}] desc[fail]",//
					new Object[] { userId, userTraceId, e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
	}
}